<?php
namespace Home\Controller;
use Think\Controller;
class MessageController extends BaseController{
	private $info_id;
	private $parent_id;
	public function __construct(){
		parent::__construct();
		if(isset($_GET['info_id'])) $this->info_id=$_GET['info_id'];
	}
	//信息详情+评论页面
	public function infodetail(){
		
		//显示信息
		$info=M("Info")
			->alias('i')
			->join('__USER__ u on i.user_id=u.user_id')
			->join('__AREA__ a on i.area_id=a.id')
			->field('i.*,u.user_name,a.area_name')
			->where("info_id=$this->info_id")
			->find();
		//处理时间
		$info['info_createtime']=date("Y-m-d", $info['info_createtime']);
		$info['info_overtime']=date("Y-m-d", $info['info_overtime']);
		//处理图片链接
		$info['info_img']=explode(',', $info['info_img']);

		//获得快速导航信息
		$cats=$this->getcats();

		//获得评论信息
		$messages=$this->getmessage();

		//获得相关信息
		$reinfos=$this->getreinfos();

		//面包屑导航
		$crumb=$this->getcrumb();

		//分配变量
		$this->assign('info',$info);
		$this->assign('cats',$cats);
		$this->assign('messages',$messages);
		$this->assign('reinfos',$reinfos);
		$this->assign('crumb',$crumb);
		$this->display();
	}


	//插入评论内容
	public function insert(){
		if (!check_code($_POST['validate'])){
			$this->error("验证码错误!");
		}
		//判断用户是否登陆并记录其id
		$data['user_id']=session('id')?session('id'):0;
		//接收并处理数据
		$data['info_id']=$_POST['info_id']+0;
		$data['mes_content']=trim($_POST['mes_content']);
		$data['mes_createtime']=time();
		$data=deepspecialchars($data);
		if (M('message')->add($data)) {
			$this->success('评论成功');
		}else{
			$this->error("评论失败!");
		}
	}


	/**
	* 获得快速导航信息
	* @return array
	*/
	private function getcats(){
		//info_id->cat_id->parent_id
		$this->parent_id=M('category')
			->alias('c')
			->join('__INFO__ i on c.cat_id=i.cat_id')
			->where("info_id=$this->info_id")
			->getField('parent_id');
		//parent_id->cats
		$cats=M('category')
			->where("parent_id=$this->parent_id")
			->field('cat_id,cat_name')
			->select();
		return $cats;
	}


	/**
	* 获得评论数据
	* @return array
	*/
	private function getmessage(){
		$messages=M('message')
			->alias('m')
			->join('__USER__ u on m.user_id=u.user_id','left')
			->field('u.user_name,m.mes_content,m.mes_createtime')
			->where(array('info_id'=>"$this->info_id"))
			->where("m.is_varify=1 or u.user_id<>0")
			->select();
		foreach ($messages as $k => $message) {
			$messages[$k]['mes_createtime']=date("Y-m-d H:i:s", $message['mes_createtime']);
		}
		return $messages;
	}


	/**
	* 获得相关信息
	* @return array
	*/
	private function getreinfos(){
		//选出相关信息的5条info_id
		$catids=M('category')
			->where("parent_id=$this->parent_id")
			->getField('cat_id',true);
		$infos=M("Info")
			->field('info_id,info_title,info_price,info_createtime,info_miniimg')
			->where(array('cat_id'=>array('in',$catids)))
			->limit(5)
			->order('info_id desc')
			->select();
		//处理时间
		foreach ($infos as $k => $info) {
			$infos[$k]['info_createtime']=date("Y-m-d", $info['info_createtime']);
		}
		return $infos;
	}

	private function getcrumb(){
		$crumb=array();
		$crumb['p']=M('category')
			->where("cat_id=$this->parent_id")
			->field('cat_id,cat_name')
			->find();
		$crumb['s']=M('category')
			->alias('c')
			->join('__INFO__ i on c.cat_id=i.cat_id')
			->where("info_id=$this->info_id")
			->field('c.cat_id,cat_name')
			->find();
		return $crumb;
	}
}